package com.ruoyi.service.impl;

import java.util.HashMap;
import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.common.utils.uuid.Seq;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.domain.*;
import com.ruoyi.mapper.HisMedicineCartMapper;
import com.ruoyi.mapper.HisMedicineDrugMapper;
import com.ruoyi.mapper.HisMedicineOrderDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.HisMedicineOrderMapper;
import com.ruoyi.service.IHisMedicineOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-31
 */
@Service
public class HisMedicineOrderServiceImpl implements IHisMedicineOrderService 
{
    @Autowired
    private HisMedicineOrderMapper hisMedicineOrderMapper;
    @Autowired
    private HisMedicineDrugMapper drugMapper;
    @Autowired
    private HisMedicineCartMapper cartMapper;
    @Autowired
    private HisMedicineOrderDetailMapper detailMapper;

    /**
     * 查询订单信息
     * 
     * @param orderId 订单信息主键
     * @return 订单信息
     */
    @Override
    public HisMedicineOrder selectHisMedicineOrderByOrderId(Long orderId)
    {
        return hisMedicineOrderMapper.selectHisMedicineOrderByOrderId(orderId);
    }

    /**
     * 查询订单信息列表
     * 
     * @param hisMedicineOrder 订单信息
     * @return 订单信息
     */
    @Override
    public List<HisMedicineOrder> selectHisMedicineOrderList(HisMedicineOrder hisMedicineOrder)
    {
        return hisMedicineOrderMapper.selectHisMedicineOrderList(hisMedicineOrder);
    }

    /**
     * 新增订单信息
     * 
     * @param
     * @return 结果
     */
    @Transactional
    @Override
    public int insertHisMedicineOrder(Long[] durgIds,Long userId,Long supperlierId) throws JsonProcessingException {
        HisMedicineOrder hisMedicineOrder = new HisMedicineOrder();
        hisMedicineOrder.setCreateTime(DateUtils.getNowDate());
        hisMedicineOrder.setOrderPerson(userId);
        hisMedicineOrder.setSupplierId(supperlierId);
        hisMedicineOrder.setOrderNum(IdUtils.randomUUID());
        hisMedicineOrder.setOrderStatus("0");
        hisMedicineOrderMapper.insertHisMedicineOrder(hisMedicineOrder);

        //拿到map中的car中的drug对象
        ObjectMapper objectMapper = new ObjectMapper();
        HisCart hisCart = cartMapper.selectCartByUserId(userId);
        String cart = hisCart.getCart();
        HashMap<Long, HisMedicineDrug> hisMedicineDrugHashMap = objectMapper.readValue(cart, new TypeReference<HashMap<Long, HisMedicineDrug>>() {
        });
        for (Long durgId : durgIds){
            HisMedicineDrug medicineDrug = hisMedicineDrugHashMap.get(durgId);
            HisMedicineOrderDetail orderDatail = new HisMedicineOrderDetail();
            orderDatail.setDetailNum(medicineDrug.getDrugCarNum());
            orderDatail.setOrderId(hisMedicineOrder.getOrderId());
            orderDatail.setDrugId(medicineDrug.getDrugId());
            detailMapper.insertHisMedicineOrderDetail(orderDatail);
            hisMedicineDrugHashMap.remove(durgId);
        }
        String s = objectMapper.writeValueAsString(hisMedicineDrugHashMap);
        hisCart.setCart(s);
        hisCart.setUserId(userId);
        return cartMapper.updateCart(hisCart);
    }

    /**
     * 修改订单信息
     * 
     * @param hisMedicineOrder 订单信息
     * @return 结果
     */
    @Override
    public int updateHisMedicineOrder(HisMedicineOrder hisMedicineOrder)
    {
        hisMedicineOrder.setUpdateTime(DateUtils.getNowDate());
        return hisMedicineOrderMapper.updateHisMedicineOrder(hisMedicineOrder);
    }

    /**
     * 批量删除订单信息
     * 
     * @param orderIds 需要删除的订单信息主键
     * @return 结果
     */
    @Override
    public int deleteHisMedicineOrderByOrderIds(Long[] orderIds)
    {
        return hisMedicineOrderMapper.deleteHisMedicineOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单信息信息
     * 
     * @param orderId 订单信息主键
     * @return 结果
     */
    @Override
    public int deleteHisMedicineOrderByOrderId(Long orderId)
    {
        return hisMedicineOrderMapper.deleteHisMedicineOrderByOrderId(orderId);
    }
}
